﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.IO;
using System.Drawing.Imaging;
using System.Net;
using CarLoanSystem.Web.Comman;
using CarLoanSystem.DAL;
using CarLoanSystem.Entity;

namespace CarLoanSystemWeb.Page.EditProfile
{
    public partial class wuc_edit_profile_dept : UserControlManageForm
    {
        public event EventHandler InfoMessege;
        public event EventHandler ErrorMessege;
        private static CustomerItem cusItem;
        private static CustomerDebtItem cusDebtItem;
        private static string mode;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cusItem = Session["Member"] as CustomerItem;
                if (this.Request.QueryString["id"] != null)
                {
                    mode = "Edit";
                    cusDebtItem = CustomerDebtInfoDAL.LoadCusDept(Convert.ToInt32(this.Request.QueryString["id"].ToString()));
                    this.txtDeptMoney.Text = cusDebtItem.DEBT_AMOUNT.ToString();
                    this.txtDetail.Text = cusDebtItem.DEBT_DETAIL;
                    this.ddlStatus.SelectedValue = cusDebtItem.CUSTOMER_DEBT_STATUS;
                }
                else 
                {
                    mode = "Add";
                    this.ddlStatus.SelectedIndex = 0;
                    this.ddlStatus.Enabled = false;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (mode == "Add")
                {
                    cusDebtItem = new CustomerDebtItem();
                    cusDebtItem.CUSTOMER_DEBT_STATUS = this.ddlStatus.SelectedValue.ToString();
                    cusDebtItem.DEBT_AMOUNT = Convert.ToDecimal(this.txtDeptMoney.Text);
                    cusDebtItem.DEBT_DETAIL = this.txtDetail.Text;
                    cusDebtItem.CUSTOMER_ID = cusItem.CUSTOMER_ID;
                    CustomerDebtInfoDAL.Insert(cusDebtItem);
                }else
                {
                    cusDebtItem.CUSTOMER_DEBT_STATUS = this.ddlStatus.SelectedValue.ToString();
                    cusDebtItem.DEBT_AMOUNT = Convert.ToDecimal(this.txtDeptMoney.Text);
                    cusDebtItem.DEBT_DETAIL = this.txtDetail.Text;
                    CustomerDebtInfoDAL.Update(cusDebtItem);
                }
                if (InfoMessege != null)
                {
                    InfoMessege(this, EventArgs.Empty);
                }
            }
            catch(Exception ex)
            {
                if (ErrorMessege != null)
                {
                    ErrorMessege(this, EventArgs.Empty);
                }
            }
        }
    }
}